package com.israelnyc.flexgooglemapdrawsquare
{
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.MapMouseEvent;
	import com.google.maps.overlays.Polygon;
	import com.google.maps.overlays.PolygonOptions;
	
	import flash.geom.Point;

	public class DrawSquare
	{
		protected var _map:Map = null;
		protected var _mapSelectionMode:Boolean = false;
		protected var _selectionPolygon:Polygon; 
		
		private var _initialSelection_X:int;
		private var _initialSelection_Y:int;
		private var _nwPoint:Point;
		private var _nePoint:Point;
		private var _sePoint:Point;
		private var _swPoint:Point;
		
		public function DrawSquare()
		{
			
		}
		
		public function init( map:Map ):void
		{
			_map = map;
			
			if( !map.isLoaded() ){
				map.addEventListener( MapEvent.MAP_READY , mapReadyHandler );
			}else{
				attachMapMouseEvents();
			}
		}
		
		private function mapReadyHandler( e:MapEvent ):void
		{
			attachMapMouseEvents();
		}
		
		private function attachMapMouseEvents():void
		{
			_map.addEventListener( MapMouseEvent.MOUSE_DOWN , mapMouseDownHandler );
			_map.addEventListener( MapMouseEvent.MOUSE_UP , mapMouseUpHandler );
		}
		
		private function mapMouseDownHandler( e:MapMouseEvent ):void
		{
			if( e.shiftKey ){
				_map.disableDragging();
				
				var viewportPoint:Point = _map.fromLatLngToViewport( e.latLng );
				_initialSelection_X = viewportPoint.x;
				_initialSelection_Y = viewportPoint.y;
				
				_map.addEventListener( MapMouseEvent.MOUSE_MOVE, mapMouseMoveHandler );
				_mapSelectionMode = true;
			}
		}
		
		private function mapMouseUpHandler( e:MapMouseEvent ):void
		{
			if( _mapSelectionMode ){
				_map.removeEventListener( MapMouseEvent.MOUSE_MOVE, mapMouseMoveHandler );
				_map.enableDragging();
				_mapSelectionMode = false;
			}
		}
		
		private function mapMouseMoveHandler( e:MapMouseEvent ):void
		{
			drawMapSelectionRect(e);
		}
		
		private function drawMapSelectionRect(e:MapMouseEvent):void
		{
			var currentPoint:Point = _map.fromLatLngToViewport(e.latLng);
			var selectionWidth:int = ( currentPoint.x - _initialSelection_X );
			var selectionHeight:int = ( currentPoint.y - _initialSelection_Y );
			var selectionPolygonLines:Array = [];
			var selectionPolygonOptions:PolygonOptions = new PolygonOptions({
				fillStyle: {
					alpha: 0.1
				}
			});
			
			
			_nwPoint = new Point( _initialSelection_X , _initialSelection_Y );
			_nePoint = new Point( (_initialSelection_X + selectionWidth) , _initialSelection_Y );
			_sePoint = new Point( (_initialSelection_X + selectionWidth) , (_initialSelection_Y + selectionHeight) );
			_swPoint = new Point( _initialSelection_X , (_initialSelection_Y + selectionHeight) );
			
			selectionPolygonLines = [
				_map.fromViewportToLatLng(_nwPoint),
				_map.fromViewportToLatLng(_nePoint),
				_map.fromViewportToLatLng(_sePoint),
				_map.fromViewportToLatLng(_swPoint),
				_map.fromViewportToLatLng(_nwPoint)
			];
			
			if( !_selectionPolygon ){
				_selectionPolygon = new Polygon( selectionPolygonLines , selectionPolygonOptions );
				_map.addOverlay( _selectionPolygon );	
			}else{
				for( var i:int = 0; i < _selectionPolygon.getPolylineCount(); i++ ){
					_selectionPolygon.removePolyline(i);
				}	
				
				_selectionPolygon.setPolyline( 0,selectionPolygonLines );
			}				
		}
	}
}